package com.cw.wizbank.util;


import com.sendgrid.Method;
import com.sendgrid.Request;
import com.sendgrid.Response;
import com.sendgrid.SendGrid;
import com.sendgrid.helpers.mail.Mail;
import com.sendgrid.helpers.mail.objects.*;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;

public class SendGridUtil {
    public static void main(String[] args) throws IOException {
//        sendMail();
        sendmail();
    }

    private static void sendMail() throws IOException {
        Email from = new Email("jinshuiyong@outlook.com");
        String subject = "Sending with Twilio SendGrid is Fun";
        Email to = new Email("2498799779@qq.com");
        Content content = new Content("text/plain", "and easy to do anywhere, even with Java");
        Mail mail = new Mail(from, subject, to, content);
        String apiKey = "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";//这个key来源参考4如何获取apiKey
        SendGrid sg = new SendGrid(apiKey);
        Request request = new Request();
        try {
            request.setMethod(Method.POST);
            request.setEndpoint("mail/send");
            request.setBody(mail.build());
            Response response = sg.api(request);
            System.out.println(response.getStatusCode());
            System.out.println(response.getBody());
            System.out.println(response.getHeaders());
            Attachments attachments = new Attachments();
            mail.addAttachments(attachments);
        } catch (Exception ex) {
            throw ex;
        }
    }

    public static void sendmail() throws IOException {

        com.sendgrid.Response response1;

        Email from = new Email("jinshuiyong@outlook.com");
        String subject = "Hello World from the SendGrid Java Library!";

        Email to = new Email("2498799779@qq.com");
        Content content = new Content("text/plain", "Hello, Email!");
        Mail mail = new Mail(from, subject, to, content);

        File file = new File("F:\\汇思工作\\工作文档\\沃尔沃\\作业批改优化.sql");
        byte[] fileData = null;
        try {
            fileData = org.apache.commons.io.IOUtils.toByteArray(new FileInputStream(file));
        } catch (IOException ex) {
        }

        Attachments attachments3 = new Attachments();
        attachments3.setContent(new String(fileData, 0, (int) file.length(), "UTF-8"));
        attachments3.setType("image/png");//"application/pdf"
        attachments3.setFilename("x.png");
        attachments3.setDisposition("attachment");
        attachments3.setContentId("Banner");
        mail.addAttachments(attachments3);


        MailSettings mailSettings = new MailSettings();
        Setting sandBoxMode = new Setting();
        sandBoxMode.setEnable(true);
        mailSettings.setSandboxMode(sandBoxMode);

        SendGrid sg = new SendGrid("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");
        Request request1 = new Request();
        try {
            request1.setMethod(Method.POST);
            request1.setEndpoint("mail/send");
            request1.setBody( mail.build());
            response1 = sg.api(request1);

            System.out.println(response1.getStatusCode());
            System.out.println(response1.getBody());
            System.out.println(response1.getHeaders());

        } catch (IOException ex) {
            System.out.println(ex);
        }
    }
}
